package com.layer8.RPS;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: a873553
 * Date: 1/2/13
 * Time: 9:42 PM
 * To change this template use File | Settings | File Templates.
 */

public class PetLoader {

    private ArrayList<Pet> petList = new ArrayList<Pet>();
    private Context mContext;

    public PetLoader(Context mContext) {
        this.mContext = mContext;
        loadPetList();
    }

    public ArrayList<Pet> getPetList() {
        return petList;
    }

    public Pet getPet(int i) {
        return petList.get(i);
    }

    public int getLength() {
        return petList.size();
    }

    public ArrayList<Pet> loadPetList() {
        petList.clear();
        AssetManager am = mContext.getAssets();
        InputStream is = null;
        try {
            is = am.open("pets.xml");
        } catch (IOException e) {
            Log.e("ERRORLOG", e.getMessage());
        }
        if (is != null) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setCoalescing(true);
            try {
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document dom = builder.parse(is);
                org.w3c.dom.Element root = dom.getDocumentElement();
                NodeList items = root.getElementsByTagName("Pet");
                for (int i = 0; i < items.getLength(); i++) {
                    String name = "", description = "";
                    int base = 0, stamina = 0, skPoint = 0, fMove = 0, image = 0;
                    Pet.AttackType fAtkType = null;
                    double strPhys = 0, strMag = 0, strGrap = 0;
                    Node item = items.item(i);
                    NodeList properties = item.getChildNodes();
                    for (int j = 0; j < properties.getLength(); j++) {
                        Node property = properties.item(j);
                        String propertyName = property.getNodeName();
                        if (propertyName.equals("Name")) {
                            name = property.getFirstChild().getNodeValue();
                        } else if (propertyName.equals("Description")) {
                            description = property.getFirstChild().getNodeValue();
                        } else if (propertyName.equals("Stamina")) {
                            stamina = Integer.parseInt(property.getFirstChild().getNodeValue());
                        } else if (propertyName.equals("Power")) {
                            base = Integer.parseInt(property.getFirstChild().getNodeValue());
                        } else if (propertyName.equals("Phys")) {
                            strPhys = Double.parseDouble(property.getFirstChild().getNodeValue());
                        } else if (propertyName.equals("Mag")) {
                            strMag = Double.parseDouble(property.getFirstChild().getNodeValue());
                        } else if (propertyName.equals("Grap")) {
                            strGrap= Double.parseDouble(property.getFirstChild().getNodeValue());
                        } else if (propertyName.equals("SkPoint")) {
                            skPoint= Integer.parseInt(property.getFirstChild().getNodeValue());
                        } else if (propertyName.equals("FAtkType")) {
                            int atkType = Integer.parseInt(property.getFirstChild().getNodeValue());
                            if (atkType == 0) {
                                fAtkType = Pet.AttackType.Physical;
                            } else if (atkType == 1) {
                                fAtkType = Pet.AttackType.Magical;
                            } else if (atkType == 2) {
                                fAtkType = Pet.AttackType.Grapple;
                            }
                        } else if (propertyName.equals("FAtkID")) {
                            fMove = Integer.parseInt(property.getFirstChild().getNodeValue());
                        } else if (propertyName.equals("Image")) {
                            String resource = property.getFirstChild().getNodeValue();
                            image = mContext.getResources().getIdentifier(resource, "drawable", mContext.getPackageName());
                        }
                    }
                    Pet pet = new Pet(name,  description, stamina, base, strPhys, strMag, strGrap, skPoint, fAtkType, fMove, image);
                    petList.add(pet);
                }
                return petList;
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }

}
